package edu.stanford.cs.cs294s;

import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class AddPoint extends Activity implements LocationListener{
	EditText mPlaceName;
	EditText mNewFact;
	Button mSubmit;
	double curLatitude;
	double curLongitude;
	LocationManager lm;
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
        setContentView(R.layout.newplace);
        
        mPlaceName = (EditText)findViewById(R.id.nameText);
        mNewFact = (EditText)findViewById(R.id.factText);
        
        mSubmit = (Button)findViewById(R.id.submitPlaceButton);
        
        lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
        lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10.0f, this);
    }

    @Override
    protected void onStart() {
    	// TODO Auto-generated method stub
    	super.onStart();
    	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10.0f, this);
    }
    
    @Override
    protected void onResume() {
    	// TODO Auto-generated method stub
    	super.onResume();
    	lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 10.0f, this);
    }
    
    @Override
    protected void onStop() {
    	// TODO Auto-generated method stub
    	super.onStop();
    	lm.removeUpdates(this);
    }
    
    @Override
    protected void onPause() {
    	// TODO Auto-generated method stub
    	super.onPause();
    	lm.removeUpdates(this);
    }
    
	@Override
	public void onLocationChanged(Location location) {
		// TODO Auto-generated method stub
		if (location != null) {
			curLatitude = location.getLatitude();
			curLongitude = location.getLongitude();
			String msg = "lat : " + curLatitude + " lon : " + curLongitude;
			Toast.makeText(getBaseContext(), msg, Toast.LENGTH_SHORT).show();
			System.err.println("LOCATION CHANGED " + msg);
		}
	}

	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
		
	}
}
